/*
 * SoundSource.cpp
 *
 *  Created on: 20 Apr 2011
 *      Author: sinan
 */

#include "SoundSource.h"

ALuint SoundSource::SoundSourceCheck;
ALuint SoundSource::SoundSourceCheckMateWin;
ALuint SoundSource::SoundSourceCheckMateLose;
ALuint SoundSource::SoundSourceTicTock;
ALuint SoundSource::SoundSourceWhistle;
ALuint SoundSource::SoundSourceWrongChessManSelection;
ChessWarningSoundType SoundSource::LastWarningSoundType;

#define NUM_BUFFERS 6
#define NUM_SOURCES 6
#define NUM_ENVIRONMENTS 1

ALfloat listenerPos[] = { 0.0, 0.0, 4.0 };
ALfloat listenerVel[] = { 0.0, 0.0, 0.0 };
ALfloat listenerOri[] = { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0 };

ALfloat source0Pos[] = { -2.0, 0.0, 0.0 };
ALfloat source0Vel[] = { 0.0, 0.0, 0.0 };

ALfloat source1Pos[] = { 2.0, 0.0, 0.0 };
ALfloat source1Vel[] = { 0.0, 0.0, 0.0 };

ALfloat source2Pos[] = { 0.0, 0.0, -4.0 };
ALfloat source2Vel[] = { 0.0, 0.0, 0.0 };

ALfloat source3Pos[] = { 0.0, 0.0, -4.0 };
ALfloat source3Vel[] = { 0.0, 0.0, 0.0 };

ALfloat source4Pos[] = { 0.0, 0.0, -4.0 };
ALfloat source4Vel[] = { 0.0, 0.0, 0.0 };

ALfloat source5Pos[] = { 0.0, 0.0, -4.0 };
ALfloat source5Vel[] = { 0.0, 0.0, 0.0 };

ALuint buffer[NUM_BUFFERS];
ALuint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];
int GLwin;

ALsizei size, freq;
ALenum format;
ALvoid *data;
ALboolean loop;
int ch;

using namespace std;

SoundSource::SoundSource() {
	// TODO Auto-generated constructor stub

}

SoundSource::~SoundSource() {
	// TODO Auto-generated destructor stub
}

void SoundSource::InitAllWarningSounds() {

	alutInit(0, NULL);

	alListenerfv(AL_POSITION, listenerPos);
	alListenerfv(AL_VELOCITY, listenerVel);
	alListenerfv(AL_ORIENTATION, listenerOri);

	alGetError(); // clear any error messages

	if (alGetError() != AL_NO_ERROR) {
		//	cout<< "- Error creating buffers !!\n";
		//		exit(1);
	} else {
		//	cout<< "init() - No errors yet.";

	}

	// Generate buffers, or else no sound will happen!
	alGenBuffers(NUM_BUFFERS, buffer);

	alutLoadWAVFile(
			const_cast<ALbyte*> (reinterpret_cast<const ALbyte*> ("src/ChessWarningSound/Check.wav")),
			&format, &data, &size, &freq, &loop);
	alBufferData(buffer[0], format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	alutLoadWAVFile(
			const_cast<ALbyte*> (reinterpret_cast<const ALbyte*> ("src/ChessWarningSound/Win.wav")),
			&format, &data, &size, &freq, &loop);
	alBufferData(buffer[1], format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	alutLoadWAVFile(
			const_cast<ALbyte*> (reinterpret_cast<const ALbyte*> ("src/ChessWarningSound/Lose.wav")),
			&format, &data, &size, &freq, &loop);
	alBufferData(buffer[2], format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	alutLoadWAVFile(
			const_cast<ALbyte*> (reinterpret_cast<const ALbyte*> ("src/ChessWarningSound/Thick.wav")),
			&format, &data, &size, &freq, &loop);
	alBufferData(buffer[3], format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	alutLoadWAVFile(
			const_cast<ALbyte*> (reinterpret_cast<const ALbyte*> ("src/ChessWarningSound/Whistle.wav")),
			&format, &data, &size, &freq, &loop); // WHISTLE
	alBufferData(buffer[4], format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	alutLoadWAVFile(
			const_cast<ALbyte*> (reinterpret_cast<const ALbyte*> ("src/ChessWarningSound/Invalid.wav")),
			&format, &data, &size, &freq, &loop);
	alBufferData(buffer[5], format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);
	// BU KISIM DEGISECEK

	alGetError(); /* clear error */
	alGenSources(NUM_SOURCES, source);

	if (alGetError() != AL_NO_ERROR) {
		cout << "- Error creating sources !!\n";
		//	exit(2);
	} else {
		//		cout << "init - no errors after alGenSources\n";
	}

	alSourcef(source[0], AL_PITCH, 1.0f);
	alSourcef(source[0], AL_GAIN, 1.0f);
	alSourcefv(source[0], AL_POSITION, source0Pos);
	alSourcefv(source[0], AL_VELOCITY, source0Vel);
	alSourcei(source[0], AL_BUFFER, buffer[0]);
	alSourcei(source[0], AL_LOOPING, AL_TRUE);

	alSourcef(source[1], AL_PITCH, 1.0f);
	alSourcef(source[1], AL_GAIN, 1.0f);
	alSourcefv(source[1], AL_POSITION, source1Pos);
	alSourcefv(source[1], AL_VELOCITY, source1Vel);
	alSourcei(source[1], AL_BUFFER, buffer[1]);
	alSourcei(source[1], AL_LOOPING, AL_TRUE);

	alSourcef(source[2], AL_PITCH, 1.0f);
	alSourcef(source[2], AL_GAIN, 1.0f);
	alSourcefv(source[2], AL_POSITION, source2Pos);
	alSourcefv(source[2], AL_VELOCITY, source2Vel);
	alSourcei(source[2], AL_BUFFER, buffer[2]);
	alSourcei(source[2], AL_LOOPING, AL_TRUE);

	alSourcef(source[3], AL_PITCH, 1.0f);
	alSourcef(source[3], AL_GAIN, 1.0f);
	alSourcefv(source[3], AL_POSITION, source3Pos);
	alSourcefv(source[3], AL_VELOCITY, source3Vel);
	alSourcei(source[3], AL_BUFFER, buffer[3]);
	alSourcei(source[3], AL_LOOPING, AL_TRUE);

	alSourcef(source[4], AL_PITCH, 1.0f);
	alSourcef(source[4], AL_GAIN, 1.0f);
	alSourcefv(source[4], AL_POSITION, source3Pos);
	alSourcefv(source[4], AL_VELOCITY, source3Vel);
	alSourcei(source[4], AL_BUFFER, buffer[4]);
	alSourcei(source[4], AL_LOOPING, AL_TRUE);

	alSourcef(source[5], AL_PITCH, 1.0f);
	alSourcef(source[5], AL_GAIN, 1.0f);
	alSourcefv(source[5], AL_POSITION, source1Pos);
	alSourcefv(source[5], AL_VELOCITY, source1Vel);
	alSourcei(source[5], AL_BUFFER, buffer[5]);
	//	alSourcei(source[5], AL_LOOPING, AL_TRUE);

	SoundSource::SoundSourceCheck = source[0];
	SoundSource::SoundSourceCheckMateWin = source[1];
	SoundSource::SoundSourceCheckMateLose = source[2];
	SoundSource::SoundSourceTicTock = source[3];
	SoundSource::SoundSourceWhistle = source[4];
	SoundSource::SoundSourceWrongChessManSelection = source[5];
	// YANLIS ADAM SECIMI SESI KONULACAK
}

void SoundSource::PlayCheck() {

	if (SoundSource::LastWarningSoundType == ChessWarningSoundType_Check) {
		// DO NOTHING
	} else {
		SoundSource::LastWarningSoundType = ChessWarningSoundType_Check;
		alSourcePlay(SoundSource::SoundSourceCheck);
		alureSleep(1.6); // TESTED
		alSourceStop(SoundSource::SoundSourceCheck);
	}
}
void SoundSource::PlayCheckMateWin() {
	SoundSource::LastWarningSoundType = ChessWarningSoundType_Win;
	alSourcePlay(SoundSource::SoundSourceCheckMateWin);
	alureSleep(1.75); // TESTED
	alSourceStop(SoundSource::SoundSourceCheckMateWin);
}
void SoundSource::PlayCheckMateLose() {
	SoundSource::LastWarningSoundType = ChessWarningSoundType_Lose;
	alSourcePlay(SoundSource::SoundSourceCheckMateLose);
	alureSleep(1.6); // TESTED
	alSourceStop(SoundSource::SoundSourceCheckMateLose);
}
void SoundSource::PlayTicTock() {
	SoundSource::LastWarningSoundType = ChessWarningSoundType_TicTac;
	alSourcePlay(SoundSource::SoundSourceTicTock);
	alureSleep(0.2); // TESTED
	alSourceStop(SoundSource::SoundSourceTicTock);
}
void SoundSource::PlayWhistle() {
	SoundSource::LastWarningSoundType = ChessWarningSoundType_Eat;
	alSourcePlay(SoundSource::SoundSourceWhistle);
	alureSleep(0.18); // ORIGINAL 0.18
	alSourceStop(SoundSource::SoundSourceWhistle);
}
void SoundSource::PlayWrongChessManSelection() {
	SoundSource::LastWarningSoundType = ChessWarningSoundType_WrongMove;
	alSourcePlay(SoundSource::SoundSourceWrongChessManSelection);
	alureSleep(0.20); // TESTED
	alSourceStop(SoundSource::SoundSourceWrongChessManSelection);
}
